import { ConfigEnv, defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { swc } from 'rollup-plugin-swc3'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv) => {
  const env = loadEnv(mode, process.cwd(), 'BUTTERFLY_')
  console.log(env)
  return {
    plugins: [
      vue(),
      swc({
        jsc: {
          parser: {
            syntax: 'typescript',
            dynamicImport: false,
            decorators: true
          },
          target: 'es2016',
          transform: {
            decoratorMetadata: true
          }
        }
      })
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, './src')
      }
    },
    envPrefix: 'BUTTERFLY_',
    base: './',
    server: {
      host: '0.0.0.0',
      cors: true
    },
    esbuild: false,
    build: {
      target: 'es2016',
      cssCodeSplit: true,
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: mode === 'production',
          drop_debugger: mode === 'production'
        },
        output: {
          comments: true // 移除注释
        }
      },
      rollupOptions: {
        output: {
          // manualChunks: {
          //   vue: ['vue', 'vue-router', 'pinia'],
          // },
          chunkFileNames: 'assets/js/[name]-[hash].js',
          entryFileNames: 'assets/js/[name]-[hash].js',
          assetFileNames: 'assets/[ext]/[name]-[hash].[ext]'
        }
      }
    }
  }
})
